var pieChat;
		
$(function(){
	getAjaxData();
})

function getAjaxData(){
	$.ajax({
		url: "/houseInfo/getAllHouse",
		type: "post",
		dataType: "json",
		success: function (json) {
			if(json.success){
				var mapReturn = json.attributes;
				var pieExsit = mapReturn.pieExsit;
				if(!pieExsit || dataIsZero(mapReturn.pieChatY)){
					$("#pie").hide();
				}else{
					$("#pie").show();
					pydata = setYdataOfPie(mapReturn.pieChatX, mapReturn.pieChatY);
					var pieOption = setPieOption("各个城市房屋信息占比", mapReturn.pieChatX, pydata, "数量");
					pieChat = myChart('echartPieArea', pieOption);
				}
			}
		}
	});
}

function dataIsZero(array){
	if(array != undefined){
		var data = eval(array);
		var sum = 0;
		for(var i = 0; i < data.length; i++){
			if(data[i] == 0){
				sum++;
			}
		}
		if(sum == data.length){
			return true;
		}
	}
	return false;
}

function setPieOption(title, xdata, ydata, name){
	var option = {
		title : {text: title, x:'center'},
		tooltip : {trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)"},
		legend: { type: 'scroll', orient: 'vertical', right: 10, top: 20, bottom: 20, data: eval(xdata),
			formatter: function (name) {
				return name.length < 15 ? name : name.substring(0,15) + '...'
			}},
		series : [{
			name: name, type: 'pie', radius : '55%', center: ['40%', '50%'], 
				data: ydata,
				itemStyle: {
					emphasis: {shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)'}
				},
				label:{
					normal:{
						formatter (v) {
							let text = v.data.name;
							return text.length < 10 ? text : text.substring(0,10) + '...'
						}
					}  
				}
		}]
	};
	return option;
}

function setYdataOfPie(pchatx, pchaty){
	if(pchatx != undefined && pchaty != undefined){
		var cols = eval(pchatx);
		var rows = eval(pchaty);
		var res = new Array();
		for ( var i = 0; i < cols.length; i++) {
			res.push("{name : \"" + cols[i].replace(/\"/g, "\\\"") + "\",value : \"" + rows[i] + "\"}");
		}
		return eval("[" + res.join(",") + "]");
	}
}

function myChart(id, option){
	var myChart = echarts.init(document.getElementById(id));
	myChart.setOption(option);
	return myChart;
}

$(window).resize(function(){
	resize();
});	

function resize(){
	if(!$("#pie").is(":hidden")){
		pieChat.resize();
	}
}